package LeetCode;

import java.util.ArrayList;
import java.util.List;

public class LC_257_BinaryTreePaths {

    public static void main(String[] args) {

    }

    public class Solution {
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        List<String> binaryTreePaths(TreeNode root) {
            if (root == null) return result;

            int cntr = sb.length();

            if (root.left == null && root.right == null) {
                sb.append(root.val);
                result.add(sb.toString());
                sb.delete(cntr, sb.length());
            }
            if (root.left != null) {
                sb.append(root.val);
                sb.append("->");
                binaryTreePaths(root.left);
                sb.delete(cntr, sb.length());
            }
            if (root.right != null) {
                sb.append(root.val);
                sb.append("->");
                binaryTreePaths(root.right);
                sb.delete(cntr, sb.length());
            }
            return result;
        }

    }

    static class Solution_2 {
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        List<String> binaryTreePaths(TreeNode root) {
            if (root == null) return result;

            int cntr = sb.length();

            if (root.left == null && root.right == null) {
                sb.append(root.val);
                result.add(sb.toString());
                sb.delete(cntr, sb.length());
            }
            if (root.left != null) {
                sb.append(root.val);
                sb.append("->");
                binaryTreePaths(root.left);
                sb.delete(cntr, sb.length());
            }
            if (root.right != null) {
                sb.append(root.val);
                sb.append("->");
                binaryTreePaths(root.right);
                sb.delete(cntr, sb.length());
            }
            return result;
        }
    }

}